from django.shortcuts import render_to_response
from django.http import HttpResponse
from django.http import HttpResponseServerError
from django.http import HttpResponseNotFound
from django.template import RequestContext
from django import newforms as forms
from data import models
from data import importer
import django


def show_importer(request):
	return render_to_response('base_import.html', {}, RequestContext(request));

def import_data(request):
	if 'data' in request.FILES:
		try:
			data_string=request.FILES['data']['content']
			stats=importer.importData(data_string)
#		except DeadlineExceededError:
#			return HttpResponseServerError('Deadline exceeded.')
		except django.utils.datastructures.MultiValueDictKeyError:
			return HttpResponseNotFound('Invalid params.')
		except models.MissingModelError, name:
			return HttpResponseNotFound('Invalid model name: "%s".' % name)
		else:
			return HttpResponse('Ok. (%s added, %s updated)' % (stats['added'], stats['refreshed']))
	else:
		return HttpResponseNotFound('No data.')

